<?php


namespace App\Models\Traits;


use App\Models\Comment;
use App\Providers\PostComment;

trait CommentTrait
{
    public function comment($content, $userId)
    {
        $comment =  $this->comments()->create(['user_id' => $userId, 'content' => $content]);
        event(new PostComment($comment));
        return $comment;
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commented');
    }

    public function getCommentsCountAttribute()
    {
        return $this->comments()->count();
    }

    public function getCommentEndpointAttribute()
    {
        return "/$this->table/$this->id/comments";
    }
}
